﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WorkOrders.Domain.BLL;
using WorkOrders.Domain.Dtos;

namespace WorkOrders.Service
{
    public class WorkOrdersSevice : IWorkOrdersSevice
    {
        public void AddWorkOrder(int customerId, string engineer)
        {
            MessageQueue queue = GetQueue(engineer);
            var customer = new CustomerManager().GetCustomer(customerId);
            var workOrder = new WorkOrderDto
                          {
                              CustomerId = customer.Id,
                              Engineer = engineer,
                              Status = customer.Status,
                              CustomerDetails = customer.Details,
                          };
            var message = new Message(workOrder);
            queue.Send(message);
            queue.Close();
        }

        private static MessageQueue GetQueue(string engineer)
        {
            var queueName = string.Format(@".\Private$\{0}", engineer);
            return !MessageQueue.Exists(queueName) ? MessageQueue.Create(queueName, true) : new MessageQueue(queueName);
        }
    }
}
